package Exer9;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.function.Function;

public class GenericConverter {
    public static <T, R> DataContainer<R> convert(DataContainer<T> source, Function<T, R> mapper) {
        DataContainer<R> resultContainer = new DataContainer<>();
        for (T element : source.toList()) {
            R mappedElement = mapper.apply(element);
            resultContainer.add(mappedElement);
        }
        return resultContainer;
    }

    public static <T> DataContainer<T> reverse(DataContainer<T> source) {
        List<T> sourceList = source.toList();
        List<T> reversedList = new ArrayList<>(sourceList);
        Collections.reverse(reversedList);
        return new DataContainer<>(reversedList);
    }

    public static <T extends Comparable<? super T>> DataContainer<T> sort(DataContainer<T> source) {
        List<T> sourceList = source.toList();
        List<T> sortedList = new ArrayList<>(sourceList);
        Collections.sort(sortedList);
        return new DataContainer<>(sortedList);
    }

    public static <T> DataContainer<T> sort(DataContainer<T> source, Comparator<? super T> comparator) {
        List<T> sourceList = source.toList();
        List<T> sortedList = new ArrayList<>(sourceList);
        Collections.sort(sortedList, comparator);
        return new DataContainer<>(sortedList);
    }
}
